//
//  MineMainListTableViewCell.m
//  BSNL_ShoppingMarket
//
//  Created by ayi on 2018/7/18.
//  Copyright © 2018年 BSNL. All rights reserved.
//

#import "MineMainListTableViewCell.h"

#import <Masonry/Masonry.h>
@interface MineMainListTableViewCell()



@end

@implementation MineMainListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


/**

 */
+ (MineMainListTableViewCell *)initWithStyleWithTab:(UITableView *)tab andreuseIdentifier:(NSString *)reuseIdentifier{
    MineMainListTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[MineMainListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        _leftimgv = [[UIImageView alloc]init];
        
        [self.contentView addSubview:self.textLab];
        [self.contentView addSubview:self.leftimgv];
        [self.contentView addSubview:self.detailLab];
        [self.contentView addSubview:self.underlineView];
        [self.contentView addSubview:self.arrowImgv];
        
        [_leftimgv mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            make.left.mas_offset(20);
            make.height.mas_equalTo(0);
            make.width.mas_equalTo(0);
        }];
        
        [_textLab mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            make.left.equalTo(self.leftimgv.mas_right).offset(14);
            make.height.mas_equalTo(self.textLab.font.lineHeight);
        }];
        [_textLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        
        [_arrowImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            make.right.mas_offset(-20);
            make.height.mas_equalTo(16);
            make.width.mas_equalTo(16);
        }];
        
        [_detailLab mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            make.left.equalTo(self.textLabel.mas_right).offset(10);
            make.height.mas_equalTo(self.detailLab.font.lineHeight);
            make.right.equalTo(self.arrowImgv.mas_left).mas_offset(-10);
        }];
        [_detailLab setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
        
        [_underlineView mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.bottom;
            make.left.mas_offset(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(.5);
        }];
        
        [self addRAC];
    }
    return self;
}
- (void)addRAC{
    @weakify(self);
    [RACObserve(self, arrowImgv.hidden) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if ([x boolValue]) {
            [self.arrowImgv mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(0);
                make.height.mas_equalTo(0);
            }];
        }else{
            [self.arrowImgv mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(self.arrowImgv.image.size.width);
                make.height.mas_equalTo(self.arrowImgv.image.size.height);
            }];
        }
    }];
    
    [RACObserve(self.leftimgv, image) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        if (x == nil) {
            [self.leftimgv mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(5);
                make.height.mas_equalTo(0);
                make.width.mas_equalTo(0);
            }];
        }else{
            [self.leftimgv mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(15);
                make.height.mas_equalTo(ceilf(FONT_SIZE(14).lineHeight) + 4);
                make.width.mas_equalTo(ceilf(FONT_SIZE(14).lineHeight) + 4);
            }];
        }
    }];
}

/**
 右边为图像的

 */
+ (MineMainListTableViewCell *)initWithStyWithImgleWithTab:(UITableView *)tab andreuseIdentifier:(NSString *)reuseIdentifier{
    MineMainListTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[MineMainListTableViewCell alloc]initWithStyleWithImg:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}

- (instancetype)initWithStyleWithImg:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.textLab];
        [self.contentView addSubview:self.headerImgv];
        [self.contentView addSubview:self.underlineView];
        
        [_textLab mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            make.left.mas_offset(15);
            make.height.mas_equalTo(self.textLab.font.lineHeight);
        }];
        [_textLab setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        
        [_headerImgv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_offset(-15);
            (void)make.centerY;
            make.height.mas_equalTo(50);
            make.width.mas_equalTo(50);
        }];
        _headerImgv.layer.masksToBounds = YES;
        _headerImgv.layer.cornerRadius = 25;
        
        [_underlineView mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.bottom;
            make.left.mas_offset(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(.5);
        }];
    }
    return self;
}

/**
 右边为switch的
 
 */
+ (MineMainListTableViewCell *)initWithStyleWithSwitchWithTab:(UITableView *)tab andreuseIdentifier:(NSString *)reuseIdentifier{
    MineMainListTableViewCell *cell = [tab dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[MineMainListTableViewCell alloc]initWithStyleWithSwitch:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyleWithSwitch:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self.contentView addSubview:self.textLab];
        [self.contentView addSubview:self.switchAction];
        [self.contentView addSubview:self.underlineView];
        
        [_switchAction addTarget:self action:@selector(switchVauleChangeAction:) forControlEvents:UIControlEventValueChanged];
        
        [_textLab mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            make.left.mas_offset(15);
            make.height.mas_equalTo(self.textLab.font.lineHeight);
        }];
        [_textLab setContentCompressionResistancePriority:UILayoutPriorityRequired  forAxis:UILayoutConstraintAxisHorizontal];
        
        [_switchAction mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.centerY;
            make.right.mas_offset(-15);
        }];
        
        [_underlineView mas_makeConstraints:^(MASConstraintMaker *make) {
            (void)make.bottom;
            make.left.mas_offset(15);
            make.right.mas_equalTo(-15);
            make.height.mas_equalTo(.5);
        }];
    }
    return self;
}

- (void)switchVauleChangeAction:(UISwitch *)oneswitch{
    if (self.switchVauleChangeCallBlock) {
        self.switchVauleChangeCallBlock(self,oneswitch.isOn);
    }
}






- (UILabel *)textLab{
    if (!_textLab) {
        _textLab = [UILabel customInitLabWithFont:16 andTextColor:RGB(51, 51, 51) andAlign:NSTextAlignmentLeft withBgColor:self.backgroundColor];
    }return _textLab;
}

- (UILabel *)detailLab{
    if (!_detailLab) {
        _detailLab = [UILabel customInitLabWithFont:12 andTextColor:Common_TextLowColor andAlign:NSTextAlignmentRight withBgColor:self.backgroundColor];
    }return _detailLab;
}

- (UIView *)underlineView{
    if (!_underlineView) {
        _underlineView = [[UIView alloc]init];_underlineView.backgroundColor = [CommonMethod colorFromHexString:@"#fafafa"];
    }return _underlineView;
}

- (UIImageView *)arrowImgv{
    if (!_arrowImgv) {
        _arrowImgv = [[UIImageView alloc]initWithImage:IMG(@"home_arrow")];
        _arrowImgv.layer.masksToBounds = YES;
    }return _arrowImgv;
}

- (UIImageView *)headerImgv{
    if (!_headerImgv) {
        _headerImgv = [[UIImageView alloc]init];
        _headerImgv.contentMode = UIViewContentModeScaleAspectFill;
        _arrowImgv.layer.masksToBounds = YES;
    }return _headerImgv;
}

- (UISwitch *)switchAction{
    if (!_switchAction) {
        _switchAction = [[UISwitch alloc]init];
    }return _switchAction;
}
@end
